﻿using System;
using System.Collections.Generic;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;


namespace KPIS.GERP.GIMS.DAL
{
    public class D_ReceiveType
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        public IList<M_ReceiveType> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ReceiveType> infoList = new List<M_ReceiveType>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT receive_type_seq,receive_type_code,receive_type_name,record_status "
                        +" ,created_when,created_by,updated_when,updated_by "
                        +" FROM icm_mas_receive_type "
                        +" WHERE record_status <>'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ReceiveType info = new M_ReceiveType();
                        info.receive_type_seq = Convert.ToInt32(drc[i]["receive_type_seq"]);
                        info.receive_type_code = drc[i]["receive_type_code"].ToString();
                        info.receive_type_name = drc[i]["receive_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<M_ReceiveType> GetListFillByName(string receive_type_name)
        {
            string strSQL = "";
            DataRowCollection drc = null;
            IList<M_ReceiveType> infoList = new List<M_ReceiveType>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT receive_type_seq,receive_type_code,receive_type_name,record_status "
                        + " ,created_when,created_by,updated_when,updated_by "
                        + " FROM icm_mas_receive_type "
                        + " WHERE record_status <>'D' and receive_type_name LIKE '%' '" + receive_type_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ReceiveType info = new M_ReceiveType();
                        info.receive_type_seq = Convert.ToInt32(drc[i]["receive_type_seq"]);
                        info.receive_type_code = drc[i]["receive_type_code"].ToString();
                        info.receive_type_name = drc[i]["receive_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }

        public M_ReceiveType GetListFillBySeq(Nullable<Int32> receive_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ReceiveType info = new M_ReceiveType();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@receive_type_seq", receive_type_seq);

                strSQL = " SELECT receive_type_seq,receive_type_code,receive_type_name,record_status "
                        + " ,created_when,created_by,updated_when,updated_by "
                        + " FROM icm_mas_receive_type "
                        + " WHERE record_status <>'D' and receive_type_seq = @receive_type_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.receive_type_seq = Convert.ToInt32(drc[0]["receive_type_seq"]);
                    info.receive_type_code = drc[0]["receive_type_code"].ToString();
                    info.receive_type_name = drc[0]["receive_type_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert_Receive_Type(M_ReceiveType info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);
                dbManager.AddParameters(0, "@receive_type_code", info.receive_type_code);
                dbManager.AddParameters(1, "@receive_type_name", info.receive_type_name);
                dbManager.AddParameters(2, "@record_status", info.record_status);
                dbManager.AddParameters(3, "@created_when", info.created_when);
                dbManager.AddParameters(4, "@created_by", info.created_by);

                strSQL = " INSERT INTO icm_mas_receive_type "
                        + " (receive_type_code,receive_type_name,record_status "
                        + " ,created_when,created_by )"
                        + "  VALUES "
                        + " (@receive_type_code "
                        + " ,@receive_type_name "
                        + " ,@record_status "
                        + " ,@created_when "
                        + " ,@created_by ) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update_Receive_Type(M_ReceiveType info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);
                dbManager.AddParameters(0, "@receive_type_seq", info.receive_type_seq);
                dbManager.AddParameters(1, "@receive_type_code", info.receive_type_code);
                dbManager.AddParameters(2, "@receive_type_name", info.receive_type_name);
                dbManager.AddParameters(3, "@record_status", info.record_status);
                dbManager.AddParameters(4, "@updated_when", info.updated_when);
                dbManager.AddParameters(5, "@updated_by", info.updated_by);

                strSQL = " UPDATE  icm_mas_receive_type "
                         + " SET  "
                         + "  receive_type_code = @receive_type_code "
                         + " ,receive_type_name = @receive_type_name "
                         + " ,record_status = @record_status "
                         + " ,updated_by = @updated_by "
                         + " ,updated_when = @updated_when "
                         + " WHERE receive_type_seq = @receive_type_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }



    }
}
